//按钮全选
var apiWebHost = "http://www.zcxy360.com/sandtable";
var apiWebsorct = "ws://www.zcxy360.com/sandtable";

var ComFunJS={	
	 isIE : function(ver){
		ver = ver || '';
		var b = document.createElement('b')
		b.innerHTML = '<!--[if IE ' + ver + ']>1<![endif]-->'
		return b.innerHTML === '1'
	}, 
//	WebSocket:"ws://192.168.72.180:8080/hatch3/myHandler/tuzhi",
//	Tuisocket:"ws://192.168.72.180:8080/hatch3/myHandler/tuiyan",
    APIURL: function (action) {
        return apiWebHost + action;
    },	
    WEBSORCTAPIURL: function (action) {
        return apiWebsorct + action;
    },	    
	hover:function (){
			$('.td-hover').mouseover(function(){
				$(this).next('.td-show').css('display','block');
				$(this).mouseleave(function(){
					$(this).next('.td-show').css('display','none');					
				})
			})
	},
	jiaoyan:function(str){
      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;  
      if (!myreg.test(str)) {   
          return false;  
      } else {  
          return true;  
      }  
	},
    showStatus: function (s) {
        var st = "";
        switch (s) {
            case '1':
                st = "消费者体验流程图";
                break;
            case '2':
                st = "典型人物图";
                break;
            case '3':
                st = "服务蓝图";
                break;
            case '4':
                st = "商业模式画布";
                break;
            case '5':
                st = "电梯演讲图";
                break;
            case '6':
                st = "洞察分析图";
                break;
            case '7':
                st = "思维导图";
                break;                
        }
        return st;
    },	
	getFileName:function (path) {  
	    var pos1 = path.lastIndexOf('/');  
	    var pos2 = path.lastIndexOf('\\');  
	    var pos = Math.max(pos1, pos2);  
	    if (pos < 0) {  
	        return path;  
	    }  
	    else {  
	        return path.substring(pos + 1);  
	    }  
	},
	load:function(){
		var loadcontent=   ' <div class="load-content">'+
			        '<div class="spiner-example">'+
			            '<div class="sk-spinner sk-spinner-cube-grid">'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			                '<div class="sk-cube"></div>'+
			            '</div>'+
			        '</div>'
		$('body').append(loadcontent)	    
	},
	thumbnail:function(src,callback,w,h){
	    var canvas = document.createElement("canvas"),
	        ctx = canvas.getContext("2d"),
	        im = new Image();
	        w = w || 0,
	        h = h || 0;
	    im.onload = function(){
	        //为传入缩放尺寸用原尺寸
	        !w && (w = this.width);
	        !h && (h = this.height);
	        //以长宽最大值作为最终生成图片的依据
	        if(w !== this.width || h !== this.height){
	            var ratio;
	            if(w>h){
	                ratio = this.width / w;
	                h = this.height / ratio;
	            }else if(w===h){
	                if(this.width>this.height){
	                    ratio = this.width / w;
	                    h = this.height / ratio;
	                }else{
	                    ratio = this.height / h;
	                    w = this.width / ratio;
	                }
	            }else{
	                ratio = this.height / h;
	                w = this.width / ratio;
	            }
	        }
	        //以传入的长宽作为最终生成图片的尺寸
	        if(w>h){
	            var offset = (w - h) / 2;
	            canvas.width = canvas.height = w;
	            ctx.drawImage(im,0,offset,w,h);
	        }else if(w<h){
	            var offset = (h - w) / 2;
	            canvas.width = canvas.height = h;
	            ctx.drawImage(im,offset,0,w,h);
	        }else{
	            canvas.width = canvas.height = h;
	            ctx.drawImage(im,0,0,w,h);
	        }
	        callback(canvas.toDataURL("image/png"));
	    }
	    im.src = src;
	},
	loadtext:function(){
		var loadtextcontent=  '<div class="ibox-content iboxanmite">' +
                        '<div class="spiner-example">'+
                            '<div class="sk-spinner sk-spinner-fading-circle">'+
                                '<div class="sk-circle1 sk-circle"></div>'+
                                '<div class="sk-circle2 sk-circle"></div>'+
                                '<div class="sk-circle3 sk-circle"></div>'+
                                '<div class="sk-circle4 sk-circle"></div>'+
                                '<div class="sk-circle5 sk-circle"></div>'+
                                '<div class="sk-circle6 sk-circle"></div>'+
                                '<div class="sk-circle7 sk-circle"></div>'+
                                '<div class="sk-circle8 sk-circle"></div>'+
                                '<div class="sk-circle9 sk-circle"></div>'+
                                '<div class="sk-circle10 sk-circle"></div>'+
                                '<div class="sk-circle11 sk-circle"></div>'+
                                '<div class="sk-circle12 sk-circle"></div>'+
                            '</div>'+
                        '</div>'+
                        '<p style="text-align: center;font-size:18px;color: #1ab394;width:100%;position: absolute;top:30%;left:0">'+'正在上传'+'</p>'+
                    '</div>'
		$('body').append(loadtextcontent)	    		
	},
	loadclose:function(){
		setTimeout(function(){
			$('.load-content').hide('')
			$('.iboxanmite').hide('')
		},0)
	},
	quancuan:function(){
		$("#checkall").click(function(){   
	    	if(this.checked){   
	        	$("tbody :checkbox").prop("checked", true);  
	    	}else{   
			$("tbody :checkbox").prop("checked", false);
	    	}   
	 	}); 	
	},
	FilePath: function (fileid, wh) {
        fileid = fileid + "";
        if (!fileid || fileid == "") {
            return "";
        }
        //如果是http路径，则直接返回
        if (fileid.toLowerCase().indexOf("http:") > -1) {
            return fileid;
        }
        if (fileid) {
            if (wh) {
                return apiWebHost + "File/thum/" + fileid.split(',')[0] + wh;//缩略图,默认200*200，可以传宽高/200/200
            }
            else {
                return apiWebHost + "File/thum/" + fileid.split(',')[0];
            }
        }
        else {
            return "";
        }
    },
    animationHover:function(element, animation) {
    element = $(element);
    element.hover(
        function () {
            element.addClass('animated ' + animation);
        },
        function () {
            //动画完成之前移除class
            window.setTimeout(function () {
                element.removeClass('animated ' + animation);
            }, 2000);
        });
	},
    getQueryString: function (name, defauval) {//获取URL参数,如果获取不到，返回默认值，如果没有默认值，返回空格
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) { return unescape(decodeURI(r[2])); }
        else {
            return defauval || "";
        }
    },
    setCookie: function (name, value, t) {  //设置cookie，默认保存7天
        var Days = 7;
        var exp = new Date();
        if (t) {
            exp.setTime(exp.getTime() + parseInt(t) * 24 * 60 * 60 * 1000);
        } else {
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        }
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    },
    getCookie: function (name) {//获取cookie
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    },
    delCookie: function (name) { //删除cookie
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = ComFunJS.getCookie(name);
        if (cval != null) {
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/;";
        }
    },
    IsLogin: function () {
        if (ComFunJS.getNowUser()) {
            ComFunJS.setNowUser(ComFunJS.getNowUser());
            return true;
        } else {
            return false;
        }
    },  
	//YDUI控件扩展
    //警告
    showAlert: function (msg, callback) {
        var msg = msg || "出错了";
        YDUI.dialog.alert(msg, function () {
            return callback && callback.call(this);
        });
    },
    //信息提示，默认2秒消失
    showMsg: function (msg,type,callback) {
        var msg = msg || "提示";
        var callback = callback;

        if (typeof type === "function") {
            callback = type;
            type = 'none';
        }

        YDUI.dialog.toast(msg, type, 2000, function () {
            return callback && callback.call(this);
        })
    },
    stopevent:function(e){
		if (e && e.stopPropagation) {
		　　e.stopPropagation(); 
		} 
		else {
			window.event.cancelBubble = true; 
		} 
    },        
    //确认
    showConfirm: function (msg,title,yes) {
        var title = title || "提示";
        YDUI.dialog.confirm(title, msg, function () {
            return yes && yes.call(this);
        });

    },
    //加载中
    loading: function (msg) {
        var msg = msg || "努力加载中";
        YDUI.dialog.loading.open(msg);
    },
    //关闭layer
    close: function (lyindex) {
        setTimeout("YDUI.dialog.loading.close();", 500);
    },    
    //获取Token
    GetToken: function () {
        var token = ComFunJS.getNowUser();
        if (!token || token == "") {
       	   window.location.href='login.html' 
        }
        return token;
    },
    getNowUser: function () {
        return ComFunJS.getCookie("admintoken");
    }, 
    setNowUser: function (obj) {
        if (ComFunJS.getCookie("admintoken")) {
            ComFunJS.delCookie("admintoken");
        }
        ComFunJS.setCookie("admintoken", obj, 7);
    },
    delNowUser: function () {
        ComFunJS.delCookie("admintoken");
    },    
    getJSON: function (url, data, callback) {
        if (typeof (data) == "function") {
            callback = data;
            data = null;
        }
        $.ajax({
            type: "GET",
            url: url,
	        cache: false,
	        async: false,
            dataType: "json",
            data: data,
		    crossDomain: true,
		    xhrFields: {
		        withCredentials: true
		    },
            success: function (result) {
                return callback & callback.call(this, result);
            }
        });
    },
    postflag:false,
    //Post方式访问带验证的API
    postJSON: function (url, data, callback) {
    	if(ComFunJS.postflag){
    		return
    	}
    	ComFunJS.postflag=true;
        if (typeof (data) == "function") {
            callback = data;
            data = null;
        }
        $.ajax({
            type: "POST",
            url: url,
	        cache: false,
	        async: false,
            dataType: "json",
            data: data,
		    crossDomain: true,
		    xhrFields: {
		        withCredentials: true
		    },
            success: function (result) {
            	ComFunJS.postflag=false;
                return callback & callback.call(this, result);
            }
        });
    },
    putJSON: function (url, data, callback) {
    	if(ComFunJS.postflag){
    		return
    	}
    	ComFunJS.postflag=true;
        if (typeof (data) == "function") {
            callback = data;
            data = null;
        }
        $.ajax({
            type: "PUT",
            url: url,
	        cache: false,
	        async: false,
            dataType: "json",
            data: data,
		    crossDomain: true,
		    xhrFields: {
		        withCredentials: true
		    },
            success: function (result) {
            	ComFunJS.postflag=false;
                return callback & callback.call(this, result);
            }
        });
    },   
    showFormBtn: function (title, url, width, height, btcallbact) {
        var width = width || $(window).width() - 300;
        var height = height || $(window).height();
        var optionwin = {
            type: 2,
            fix: true, //不固定
            area: [width + 'px', height + 'px'],
            maxmin: true,
            content: url,
            title: title,
            shadeClose: false,
            success: function (layero, index) {
                if ($(layero).find(".successfoot").length == 0) {
                    var footdv = $('<div class="layui-layer-title successfoot" style="border-bottom-width: 1px; padding: 0 20px 0 10px;margin-top: -3px;height:50px"></div>');
                    var btnConfirm = $("<a href='javascript:;' class='layui-btn layui-btn-normal' style='float:right; margin-top: 10px;'><i class='fa fa-spinner fa-spin' style='display:none'></i> 确   认</a>");
                    var btnCancel = $("<a href='javascript:;' class='layui-btn layui-btn-primary' style='float:right; margin-top: 10px;margin-right: 10px;'>取  消</a>");
                    var msg = $("<input type='hidden' class='r_data' >");

                    btnConfirm.appendTo(footdv).bind('click', function () {
                        return btcallbact.call(this, layero, index, btnConfirm);
                    })
                    btnCancel.appendTo(footdv).bind('click', function () {
                        layer.close(index)
                    })
                    $(layero).append(footdv).append(msg);

                    try {
                    } catch (e) { }
                }

            }
        }
        layer.open(optionwin);
    },    
    fileUpload: function (callback) {
        ComFunJS.showFormBtn("请选择文件", "../fileupload.html", "500", "400", function (layero, index, btdom) {
            var iframeWin = layero.find('iframe')[0];
            var data = iframeWin.contentWindow.getFileData();
            if (data && data.length > 0) {
                ComFunJS.postJSON(ComFunJS.APIURL('file/uploadVideo'), { data: JSON.stringify(data) }, function (result) {
                    r = result;
                    if (r.code == "200") {
                        layer.close(index);
                        return callback.call(this, r.result);
                    }
                })
            } else {
                layer.close(index);
            }

        })
    },    
}
//重写ajax方法
